package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.server.service.AudioService;
import com.tanhua.server.vo.AudioVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 桃花传音
 */
@RestController
@RequestMapping("peachblossom")
public class AudioController {
    //注入AudioService
    @Autowired
    private AudioService audioService;

    /**
     * 发送语音的方法
     *
     * @param file 语音消息
     * @return
     */
    @PostMapping
    public ResponseEntity<String> sendAudio(@RequestParam("soundFile") MultipartFile file) {
        try {
            //调用AudioService中的方法
            String s = audioService.sendAudio(file);
            if (s.equals("-1")) {
                //发送次数用完了
                return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();

            } else {
                //成功
                return ResponseEntity.ok(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //异常
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 接收语音的方法
     *
     * @return
     */
    @GetMapping
    public ResponseEntity<AudioVo> acceptAudio() {
        //调用AudioService中的方法
        AudioVo audioVo = audioService.accpetAudio();
        if (!ObjectUtil.isEmpty(audioVo)) {
            //不为空
            return ResponseEntity.ok(audioVo);
        }
        //为空
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("like")
    public ResponseEntity<Void> likeThisAudio(@RequestBody Map<String, String> param){
        //获取请求参数
        String soundUrl = param.get("soundurl");
        String sendUserId = param.get("id");
        //调用service的方法
        Boolean b=audioService.likeThisAudio(sendUserId,soundUrl);
        if (b){
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }


}
